package com.example.atomics;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.DoubleAdder;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.LongBinaryOperator;

@Slf4j
public class LongAdderAPIDemo {
    public static void main(String[] args) {
        LongAdder longAdder = new LongAdder();//只能做加法

        longAdder.increment();
        longAdder.increment();
        longAdder.increment();

        log.info("ret:{}", longAdder.longValue());

        LongAccumulator longAccumulator = new LongAccumulator(new LongBinaryOperator() {
            @Override
            public long applyAsLong(long left, long right) {
                return left - right;
            }
        }, 100);// identity 是初始值


        DoubleAdder doubleAdder = new DoubleAdder();
        doubleAdder.add(1.1);
        doubleAdder.add(1.1);
        doubleAdder.add(1.1);

        longAccumulator.accumulate(1);//1
        longAccumulator.accumulate(2);//3
        longAccumulator.accumulate(3);//6

        log.info("ret:{}", longAccumulator.longValue());
        log.info("ret:{}", doubleAdder.doubleValue());
        double d = 1.677;
        System.out.println("ret:" + d * 3);
        log.info("ret:{}", d * 3);


    }
}
